#include "bV2.hh"

using namespace std;
using boost::optional;


int main() {
  Basic basic;
  string tmp;
  shared_ptr<string> sp;

  while(getline(cin, tmp)) {
    istringstream line(tmp);
    unsigned short lineno;

    if(line >> lineno) {
      getline(line, tmp);
      TokenListType tokens(tokenise(default_keywords, tmp));
      basic.add_line(lineno, tokens);
    } else {
      TokenListType tokens(tokenise(default_keywords, tmp));
      //cout << "tokens.size() = " << tokens.size() << endl;
      //cout << "basic.vars.size() = " << basic.numvars() << endl;
      //Not very helpful...: for(auto i : tokens) cout << typeid(i.get()).name() << '\t';
      //cout << endl;
      try {
	basic.statement(tokens.begin(), tokens.end());
      }
      catch(const std::exception &error) {
	const Token *tok = basic.get_current_token();
	cerr << "Error '" << error.what() << "' at ";
	if(tok) 
	  cerr << "token '" << tok->str() << "'!";
	else
	  cerr << "end of line!";
	cerr << endl;
      }
    }
  }
  return 0;
}

/*
 * 100 print "START!"
 * 200 for i = 0 to 300
 * 300 for j = i to 300
 * 400 gosub SUBR
 * 500 next j
 * 600 next i
 * 998 print "STOP!"
 * 999 end
 * 1000 proc SUBR
 * 1100 let k = 2 * (i + j)
 * 1200 let k = k - 1
 * 1250 rem print i "," j "," k
 * 1300 if k >= 0 then goto 1200
 * 1400 return
 *
 * Runtime:
 * with list<Token*>:
 * g++ -g -std=c++0x -Wall -O2 -DNDEBUG bV2.cc: 17.53user 0.00system 0:17.54elapsed 99%CPU (0avgtext+0avgdata 5376maxresident)k
 * g++ -g -std=c++0x -Wall -O3 -DNDEBUG bV2.cc: 16.85user 0.00system 0:16.86elapsed 99%CPU (0avgtext+0avgdata 5424maxresident)k
 * 
 * Using a vector<Token*> does not really make a difference (or a very
 * small one...).
 * 
 * with list<TokenPtr>:
 * g++ -std=c++0x -Wall -O2 -DNDEBUG bV2.cc: 20.63user 0.00system 0:20.64elapsed 99%CPU (0avgtext+0avgdata 5376maxresident)k
 * g++ -std=c++0x -Wall -O3 -DNDEBUG bV2.cc: 21.44user 0.00system 0:21.45elapsed 99%CPU (0avgtext+0avgdata 5424maxresident)k
 * 
 * with vector<TokenPtr>: 
 * g++ -std=c++0x -Wall -O1 bV2.cc: 21.50user 0.00system 0:21.50elapsed 99%CPU (0avgtext+0avgdata 5424maxresident)k
 * g++ -std=c++0x -Wall -O3 -fomit-frame-pointer -DNDEBUG bV2.cc: 23.72user 0.00system 0:23.73elapsed 99%CPU (0avgtext+0avgdata 5456maxresident)k
 * g++ -std=c++0x -Wall -O2 -fomit-frame-pointer -DNDEBUG bV2.cc: 20.83user 0.00system 0:20.84elapsed 99%CPU (0avgtext+0avgdata 5472maxresident)k
 * g++ -std=c++0x -Wall -O2 -DNDEBUG bV2.cc: 20.64user 0.00system 0:20.64elapsed 99%CPU (0avgtext+0avgdata 5456maxresident)k
 * g++ -std=c++0x -Wall -O3 -DNDEBUG bV2.cc: 23.40user 0.00system 0:23.41elapsed 99%CPU (0avgtext+0avgdata 5456maxresident)k
 */ 
